var isSubmit = 0;// 0 可以提交，1 不可提交
function editPic(imgSort, isadd) {
    sessionStorage.setItem("isadd",isadd);
    sessionStorage.setItem("imgSort",imgSort);
    layer_show("修改图片","skuPic.html","1000","800");
}
function addPic(imgSort, isadd) {
    sessionStorage.setItem("isadd",isadd);
    sessionStorage.setItem("imgSort",imgSort);
    layer_show("添加图片","skuPic.html","1000","800");
}
function deletePic(imgSort) {
    var popId = layer.confirm('确认要删除吗？',function(index){
        $(".addSpuImgbox").css({
            display:"block"
        })
        var skuImgCache = JSON.parse(sessionStorage.getItem("skuImgCache"));
        var skuInfoImgArr = Object.keys(skuImgCache);
        if(sessionStorage.getItem("skuImgCache")){
            for (var i = 0; i < skuInfoImgArr.length; i++) {
                if(imgSort == (i + 1)){
                    for (var j = imgSort; j < skuInfoImgArr.length; j++) {
                        skuImgCache["img" + (j + 1)].imgSort = j;
                        skuImgCache["img" + j] = skuImgCache["img" + (j + 1)];
                    }
                    delete skuImgCache["img" + skuInfoImgArr.length];
                    break;
                }
            }
            /*if(imgSort == 1){
                if(skuImgCache.img2){
                    skuImgCache.img2.imgSort = 1;
                    skuImgCache.img1 = skuImgCache.img2;
                }
                if(skuImgCache.img3){
                    skuImgCache.img3.imgSort = 2;
                    skuImgCache.img2 = skuImgCache.img3;
                }
                if(skuImgCache.img4){
                    skuImgCache.img4.imgSort = 3;
                    skuImgCache.img3 = skuImgCache.img4;
                }
                if(skuImgCache.img5){
                    skuImgCache.img5.imgSort = 4;
                    skuImgCache.img4 = skuImgCache.img5;
                }
                switch (spuInfoImgArr.length){
                    case 1:
                        delete skuImgCache.img1;
                        console.log("-1")
                        break;
                    case 2:
                        delete skuImgCache.img2;
                        console.log("-2")
                        break;
                    case 3:
                        delete skuImgCache.img3;
                        console.log("-3")
                        break;
                    case 4:
                        delete skuImgCache.img4;
                        console.log("-4")
                        break;
                    case 5:
                        delete skuImgCache.img5;
                        console.log("-5")
                        break;
                }

            }else if(imgSort == 2){
                if(skuImgCache.img3){
                    skuImgCache.img3.imgSort = 2;
                    skuImgCache.img2 = skuImgCache.img3;
                }
                if(skuImgCache.img4){
                    skuImgCache.img4.imgSort = 3;
                    skuImgCache.img3 = skuImgCache.img4;
                }
                if(skuImgCache.img5){
                    skuImgCache.img5.imgSort = 4;
                    skuImgCache.img4 = skuImgCache.img5;
                }
                switch (spuInfoImgArr.length){
                    case 1:
                        delete skuImgCache.img1;
                        console.log("-1")
                        break;
                    case 2:
                        delete skuImgCache.img2;
                        console.log("-2")
                        break;
                    case 3:
                        delete skuImgCache.img3;
                        console.log("-3")
                        break;
                    case 4:
                        delete skuImgCache.img4;
                        console.log("-4")
                        break;
                    case 5:
                        delete skuImgCache.img5;
                        console.log("-5")
                        break;
                }
            }else if(imgSort == 3){
                if(skuImgCache.img4){
                    skuImgCache.img4.imgSort = 3;
                    skuImgCache.img3 = skuImgCache.img4;
                }
                if(skuImgCache.img5){
                    skuImgCache.img5.imgSort = 4;
                    skuImgCache.img4 = skuImgCache.img5;
                }
                switch (spuInfoImgArr.length){
                    case 1:
                        delete skuImgCache.img1;
                        console.log("-1")
                        break;
                    case 2:
                        delete skuImgCache.img2;
                        console.log("-2")
                        break;
                    case 3:
                        delete skuImgCache.img3;
                        console.log("-3")
                        break;
                    case 4:
                        delete skuImgCache.img4;
                        console.log("-4")
                        break;
                    case 5:
                        delete skuImgCache.img5;
                        console.log("-5")
                        break;
                }
            }else if(imgSort == 4){
                if(skuImgCache.img5){
                    skuImgCache.img5.imgSort = 4;
                    skuImgCache.img4 = skuImgCache.img5;
                }
                switch (spuInfoImgArr.length){
                    case 1:
                        delete skuImgCache.img1;
                        console.log("-1")
                        break;
                    case 2:
                        delete skuImgCache.img2;
                        console.log("-2")
                        break;
                    case 3:
                        delete skuImgCache.img3;
                        console.log("-3")
                        break;
                    case 4:
                        delete skuImgCache.img4;
                        console.log("-4")
                        break;
                    case 5:
                        delete skuImgCache.img5;
                        console.log("-5")
                        break;
                }
            }else if(imgSort == 5){
                delete skuImgCache.img5;
            }*/
            sessionStorage.setItem("skuImgCache",JSON.stringify(skuImgCache));
        }

        layer.close(popId);
        addSkuPic();
    });
}
function isNull () {
    if ($("#goodsName").val() == "") {
        console.log(111);
        $("#goodsName").css({
            border: "1px red solid"
        });
        $(".errorMsg").css({
            display:"inline"
        });
    }else{
        $("#goodsName").css({
            border: "1px #ddd solid"
        });
        $(".errorMsg").css({
            display:"none"
        });
    }
}

function makeSure() {
    /*校验最小起订量不能为空*/
    var miniType = $("input[name='orderLimitType']:checked").val();
    var miniOrder = document.getElementById('miniOrder').value;
    if (miniType == null || miniType == undefined || miniType == ''){
        $.Huimodalalert("最小起订量类型不能为空!", 2000);
        return;
    }
    if (miniOrder == null || miniOrder == undefined || miniOrder == ''){
        $.Huimodalalert("最小起订量不能为空!", 2000);
    }

    var skuColor = document.getElementById('skuColor').value;
    if(skuColor.trim() != null && skuColor.trim() != undefined && skuColor.trim() != ''){
        if( skuColor.trim().length > 50){
            $.Huimodalalert("颜色文本长度不能超过50!", 2000);
            return false;
        }
    }

    var skuSpecifications = document.getElementById('skuSpecifications').value;
    if(skuSpecifications.trim() != null && skuSpecifications.trim() != undefined && skuSpecifications.trim() != ''){
        if(skuSpecifications.trim().length > 50){
            $.Huimodalalert("规格文本长度不能超过50!", 2000);
            return false;
        }
    }

    var isNullSkuSpecifications = false;
    if(skuSpecifications == null || skuSpecifications == undefined || skuSpecifications == '' ){
        isNullSkuSpecifications = true;
    }

    var isNullSkuColor = false;
    if(skuColor == null || skuColor == undefined || skuColor == '' ){
        isNullSkuColor = true;
    }

    if(isNullSkuSpecifications && isNullSkuColor){
        $.Huimodalalert("请维护销售属性值信息!,规格和颜色不能同时为空", 2000);
        return false;
    }


    var skuFistResult = JSON.parse(sessionStorage.getItem("skuInfo"));
    skuFistResult.data.order_limit_type = sessionStorage.getItem("order_limit_type");
    skuFistResult.data.mini_order = sessionStorage.getItem("mini_order");
    sessionStorage.setItem("skuInfo", JSON.stringify(skuFistResult));

    if (isSubmit != 0) {
        $.Huimodalalert("排序输入有误", 2000);
        return false;
    }
    var imgSort = document.getElementsByName("imgSort");
    var sort = 0;
    var sortSum = 0;
    for (var i = 0; i < imgSort.length; i++) {
        sort += Number(imgSort[i].value);
        sortSum += i + 1;
    }
    if (sort != sortSum) {
        $.Huimodalalert("排序顺序输入有误", 2000);
        console.log("排序顺序输入有误");
        return false;
    }

    if (sessionStorage.getItem("scene_lable")) {
        var lableInfo = JSON.parse(sessionStorage.getItem("scene_lable"));
        var scene_lableArr = [];
        for (var i=0;i<lableInfo.length;i++){
            scene_lableArr.push(
                {
                    "skuId" : getQueryString('sku_Id'),
                    "lableId":lableInfo[i].lableId2
                }
            )
        }
    } else {
        var lableId1 = "";
        var lableId2 = "";
    }
    var skuResult = JSON.parse(sessionStorage.getItem("skuInfo"));

    var skuImgs = [];
    var imgLength= document.getElementsByClassName("imgboxUl")[0].children.length;
    for (var i = 1; i <= imgLength; i++) {
        if($("#skuImg" + i).children().length != 0){
            skuImgs.push( {
                "imgId": $("#skuImgId" + i).html(),
                "imgSort": $("#skuImgSort" + i).val(),
                "imgUrl": $("#skuImgUrl" + i).html().split("?")[0]
            })
        }
    }
    console.log(skuImgs);
    /*if($("#spuImg1").children().length != 0){
        spuImgs.push( {
            "imgId": $("#spuImgId1").html(),
            "imgSort":$("#number1").val(),
            "imgUrl": $("#spuImgUrl1").html().split("?")[0],
        })
    }
    if($("#spuImg2").children().length != 0){
        spuImgs.push( {
            "imgId": $("#spuImgId2").html(),
            "imgSort": $("#number2").val(),
            "imgUrl": $("#spuImgUrl2").html().split("?")[0],
        })
    }
    if($("#spuImg3").children().length != 0){
        spuImgs.push( {
            "imgId": $("#spuImgId3").html(),
            "imgSort": $("#number3").val(),
            "imgUrl": $("#spuImgUrl3").html().split("?")[0],
        })
    }
    if($("#spuImg4").children().length != 0){
        spuImgs.push( {
            "imgId": $("#spuImgId4").html(),
            "imgSort": $("#number4").val(),
            "imgUrl": $("#spuImgUrl4").html().split("?")[0],
        })
    }
    if($("#spuImg5").children().length != 0){
        spuImgs.push( {
            "imgId": $("#spuImgId5").html(),
            "imgSort": $("#number5").val(),
            "imgUrl": $("#spuImgUrl5").html().split("?")[0],
        })
    }*/

    if(sessionStorage.getItem("attrObjCanch")){
        var attrObjCanch = JSON.parse(sessionStorage.getItem("attrObjCanch"));
        var  attValList = []
        for(var i=0;i<attrObjCanch.length;i++){
            attValList.push({"attributeId": attrObjCanch[i].attribute_id, "attriValueIds": []})
            for(var j=0;j<attrObjCanch[i].value.length;j++){
                attValList[i].attriValueIds.push(attrObjCanch[i].value[j].attri_value_id);
            }
        }
    }else {
        var spuResults = JSON.parse(sessionStorage.getItem("skuInfo"));
        var attrObjCanchSku = spuResults.attrbuteValue;
        var  attValList = []
        if(attrObjCanchSku != null){
            for(var i=0;i<attrObjCanchSku.length;i++){
                attValList.push({"attributeId": attrObjCanchSku[i].attribute_id, "attriValueIds": []})
                for(var j=0;j<attrObjCanchSku[i].value.length;j++){
                    attValList[i].attriValueIds.push(attrObjCanchSku[i].value[j].attri_value_id);
                }
            }
        }
        console.log(attValList);
    }

    var JsonObject =JSON.parse(decodeURIComponent(web.readCookie("loginData")));
    $.ajax({
        contentType:'application/json',
        url: root.goodsInter.goods_updateSkuDetail,
        cache: false,  //禁用缓存
        type: "POST",
        data: JSON.stringify({
            "sku_detail": UE.getEditor('editor').getContent(),
            "spuCode": $("#spu_code").html(),
            "imgList": skuImgs,
            "spuId": skuResult.data.spu_id,
            "skuId": skuResult.data.sku_id,
            "lastModifiedBy":JsonObject.user.user_ID,
            "attValList": attValList,
            "listSkuLable": scene_lableArr,
            "mini_order" : skuResult.data.mini_order,
            "order_limit_type" : skuResult.data.order_limit_type,
            "skuCode": $("#sku_code").html(),
            "skuColor":skuColor,
            "skuSpecifications":skuSpecifications,
            "skuName" : document.getElementById('skuName').value

        }),
        success: function (data) {
            console.log(data);
            if(data.errorCode == 0){
                layer.msg("修改成功",{icon: 6,time:1000},function () {
                    sessionStorage.removeItem("skuImgCache");
                    var index =  parent.location.reload();
                    parent.layer.close(index);
                });
            }
        }
    });
}


/*if(sessionStorage.getItem("attrObjCanch")){
    var attrObjCanch = JSON.parse(sessionStorage.getItem("attrObjCanch"));
    var  attValList = []
    for(var i=0;i<attrObjCanch.length;i++){
        attValList.push({"attributeId": attrObjCanch[i].attribute_id, "attriValueIds": []})
        for(var j=0;j<attrObjCanch[i].value.length;j++){
            attValList[i].attriValueIds.push(attrObjCanch[i].value[j].attri_value_id);
        }
    }
}else {
    var spuResult = JSON.parse(sessionStorage.getItem("skuInfo"));
    console.log("-------------------" + spuResult);
    var attrObjCanch = spuResult.attrbuteValue;
    var  attValList = []
    if(attrObjCanch != null){
        for(var i=0;i<attrObjCanch.length;i++){
            attValList.push({"attributeId": attrObjCanch[i].attribute_id, "attriValueIds": []})
            for(var j=0;j<attrObjCanch[i].value.length;j++){
                attValList[i].attriValueIds.push(attrObjCanch[i].value[j].attri_value_id);
            }
        }
    }
    console.log(attValList);
}*/

//渲染属性名称
function addAttrName() {
    if (sessionStorage.getItem("attrObjCanch")) {
        var attrObjCanch = JSON.parse(sessionStorage.getItem("attrObjCanch"));
        for (var i = 0; i < attrObjCanch.length; i++) {
            var attrValStr = "";
            var attrIdsStr = "";
            if (attrObjCanch[i].value.length != 0) {
                for (var j = 0; j < attrObjCanch[i].value.length; j++) {
                    attrValStr += attrObjCanch[i].value[j].attri_value + ";";
                    attrIdsStr += attrObjCanch[i].value[j].attri_value_id + ";";
                }
            }
            $("#appendSkuAttr").remove();
            $("#attrTable").append('<tr id = "appendSkuAttr"><th class="col-6-1 text-c">' + attrObjCanch[i].attribute_name + '</th><td><span>' + attrValStr + '</span><a onclick="addAttr(' + attrObjCanch[i].attribute_id + ",'" + attrIdsStr + "'" + ')">添加</a></td></tr>')
        }
    }
}

function addAttr(attrId,checkedIds) {
    layer_show("属性值添加","addNature.html","600","400");
    sessionStorage.setItem("atrrId",attrId);
    sessionStorage.setItem("checkedIds",checkedIds);
    sessionStorage.setItem("spuOrSkuStatus","sku");
}

function nameAdd() {
    var aliasInp = document.querySelectorAll(".aliasInp")
    console.log(aliasInp)
    if(aliasInp.length<10){
        $("#inpBox").append('<div class="formControls col-sm-9 f-l aliasInp"><input type="text" class="input-text radius f-l another_name" value="" name="another_name" style="width: 85%"><a onclick="nameDelete(this)" class="ml-20 f-l">删除</a></div>');
    }
}
function addCodeFun(id,brand_name) {
    $("#tuan").hide();
    $("#username").val(brand_name)
    brandName = brand_name;
    brandId = id;
}
//渲染图片渲染函数
function addSkuPic() {
    $(".imgTable .imgboxUl").html("");
    if(sessionStorage.getItem("skuImgCache")){
        var skuInfoImg = JSON.parse(sessionStorage.getItem("skuImgCache"));
        var skuInfoImgArr = Object.keys(skuInfoImg);
        for (var i = 0; i < skuInfoImgArr.length; i++) {
            var img = "img" + (i + 1);
            $(".imgTable .imgboxUl").append('<div class="f-l ml-5"><span id="skuImg' + (i + 1) + '" class="sku_add_img"><img onclick="picShow(\'查看图片\',\'picShow.html\',\'' + skuInfoImg[img].imgUrl + '\')" src="' + skuInfoImg[img].imgUrl + '" width="150" height="150"></span><span id="skuImgId' + (i + 1) + '" style="display: none">' + skuInfoImg[img].imgId + '</span><span id="skuImgUrl' + (i + 1) + '" style="display: none">' + skuInfoImg[img].imgUrl + '</span><div id="btnBox' + (i + 1) + '" class="pt-10 updataBtn"><a onclick="editPic(' + (i + 1) + ', false)" class="pd-20">修改</a><a onclick="deletePic(' + (i + 1) + ')" class="pd-20">删除</a></div><div class="pd-20">排序&nbsp;&nbsp;&nbsp;<input type="text" class="input-text radius" onchange="checkRepeat(this, ' + (i + 1) + ')" value="' + skuInfoImg[img].imgSort + '" id="skuImgSort' + (i + 1) + '" name="imgSort" style="width:40px"></div></div>');
        }
        /*for(item in spuInfoImg) {
            if(item.endWith("1")){
                $(".imgTable .imgboxUl").append('<div class="f-l ml-5"><span id="spuImg1" class="spu_add_img"><img onclick="picShow(\'查看图片\',\'picShow.html\',\''+spuInfoImg[item].imgUrl+'\')" src="'+spuInfoImg[item].imgUrl+'" width="150" height="150"></span><span id="spuImgId1" style="display: none">'+spuInfoImg[item].imgId+'</span><span id="spuImgUrl1" style="display: none">'+spuInfoImg[item].imgUrl+'</span><span id="spuImgSort1" style="display: none">'+spuInfoImg[item].imgSort+'</span><div id="btnBox1" class="pt-10 updataBtn"><a onclick="editPic(1,false)" class="pd-20">修改</a><a onclick="deletePic(1)" class="pd-20">删除</a></div><div class="pd-20">排序&nbsp;&nbsp;&nbsp;<input type="text" class="input-text radius" onchange="checkRepeat(this, 1)" value="'+spuInfoImg[item].imgSort+'" id="number1" name="imgSort" style="width:40px"></div></div>')
            }if(item.endWith("2")){
                $(".imgTable .imgboxUl").append('<div class="f-l ml-5"><span id="spuImg2" class="spu_add_img"><img onclick="picShow(\'查看图片\',\'picShow.html\',\''+spuInfoImg[item].imgUrl+'\')" src="'+spuInfoImg[item].imgUrl+'" width="150" height="150"></span><span id="spuImgId2" style="display: none">'+spuInfoImg[item].imgId+'</span><span id="spuImgUrl2" style="display: none">'+spuInfoImg[item].imgUrl+'</span><span id="spuImgSort2" style="display: none">'+spuInfoImg[item].imgSort+'</span><div id="btnBox2" class="pt-10 updataBtn"><a onclick="editPic(2,false)" class="pd-20">修改</a><a onclick="deletePic(2)" class="pd-20">删除</a></div><div class="pd-20">排序&nbsp;&nbsp;&nbsp;<input type="text" class="input-text radius" onchange="checkRepeat(this, 2)" value="'+spuInfoImg[item].imgSort+'" id="number2" name="imgSort" style="width:40px"></div></div>')
            }if(item.endWith("3")){
                $(".imgTable .imgboxUl").append('<div class="f-l ml-5"><span id="spuImg3" class="spu_add_img"><img onclick="picShow(\'查看图片\',\'picShow.html\',\''+spuInfoImg[item].imgUrl+'\')" src="'+spuInfoImg[item].imgUrl+'" width="150" height="150"></span><span id="spuImgId3" style="display: none">'+spuInfoImg[item].imgId+'</span><span id="spuImgUrl3" style="display: none">'+spuInfoImg[item].imgUrl+'</span><span id="spuImgSort3" style="display: none">'+spuInfoImg[item].imgSort+'</span><div id="btnBox3" class="pt-10 updataBtn"><a onclick="editPic(3,false)" class="pd-20">修改</a><a onclick="deletePic(3)" class="pd-20">删除</a></div><div class="pd-20">排序&nbsp;&nbsp;&nbsp;<input type="text" class="input-text radius" onchange="checkRepeat(this, 3)" value="'+spuInfoImg[item].imgSort+'" id="number3" name="imgSort" style="width:40px"></div></div>')
            }if(item.endWith("4")){
                $(".imgTable .imgboxUl").append('<div class="f-l ml-5"><span id="spuImg4" class="spu_add_img"><img onclick="picShow(\'查看图片\',\'picShow.html\',\''+spuInfoImg[item].imgUrl+'\')" src="'+spuInfoImg[item].imgUrl+'" width="150" height="150"></span><span id="spuImgId4" style="display: none">'+spuInfoImg[item].imgId+'</span><span id="spuImgUrl4" style="display: none">'+spuInfoImg[item].imgUrl+'</span><span id="spuImgSort4" style="display: none">'+spuInfoImg[item].imgSort+'</span><div id="btnBox4" class="pt-10 updataBtn"><a onclick="editPic(4,false)" class="pd-20">修改</a><a onclick="deletePic(4)" class="pd-20">删除</a></div><div class="pd-20">排序&nbsp;&nbsp;&nbsp;<input type="text" class="input-text radius" onchange="checkRepeat(this, 4)" value="'+spuInfoImg[item].imgSort+'" id="number4" name="imgSort" style="width:40px"></div></div>')
            }if(item.endWith("5")){
                $(".imgTable .imgboxUl").append('<div class="f-l ml-5"><span id="spuImg5" class="spu_add_img"><img onclick="picShow(\'查看图片\',\'picShow.html\',\''+spuInfoImg[item].imgUrl+'\')" src="'+spuInfoImg[item].imgUrl+'" width="150" height="150"></span><span id="spuImgId5" style="display: none">'+spuInfoImg[item].imgId+'</span><span id="spuImgUrl5" style="display: none">'+spuInfoImg[item].imgUrl+'</span><span id="spuImgSort5" style="display: none">'+spuInfoImg[item].imgSort+'</span><div id="btnBox5" class="pt-10 updataBtn"><a onclick="editPic(5,false)" class="pd-20">修改</a><a onclick="deletePic(5)" class="pd-20">删除</a></div><div class="pd-20">排序&nbsp;&nbsp;&nbsp;<input type="text" class="input-text radius" onchange="checkRepeat(this, 5)" value="'+spuInfoImg[item].imgSort+'" id="number5" name="imgSort" style="width:40px"></div></div>');
                $(".addSpuImgbox").css({
                    display:"none"
                })
            }
        }*/
    }
}


/*---------------------------商品图片管理-------------------------------------------------------------------*/
function imgGoods_listCategory(categoryLevel) {
    var categoryName = $("#imgClass1").val();
    var user_data = {
        categoryLevel: categoryLevel,
        categoryName: $("#imgClass1").val(),
    }
    var succCallBack = function (data, status, response) {
        console.log(data)
        if (data.errorCode == "0") {
            if (data.data == null) {
                classStr = "<li>无数据</li>"
                $("#img-classUl1").html(classStr);
            } else {
                var classStr = "";
                for (var i = 0; i < data.data.length; i++) {
                    classStr += "<li onclick='imgNextClass(" + data.data[i].category_id + "," + data.data[i].category_level + ",\"" + data.data[i].category_name + "\")'>" + data.data[i].category_name + "</li>"
                }
                $("#img-classUl1").html(classStr);
            }
        }
    }
    web.doAjax(root.goodsInter.categoryByParam, 'post', user_data, succCallBack);
}


//获取光标
$("#imgClass1").focus(function () {
    $("#img-classUl1").show();
    imgGoods_listCategory(1, 1);
});
$(document).bind("click", function (e) {
//id为menu的是菜单，id为open的是打开菜单的按钮
    if ($(e.target).closest("#img-classUl1").length == 0 && $(e.target).closest("#imgClass1").length == 0) {
//点击id为menu之外且id不是不是open，则触发
        $("#img-classUl1").hide()
    }
});
var oneCategoryId = "";
var twoCategoryId = "";
var threeCategoryId = "";

// 2,3,4级的模糊查询
function imgGetNext_input(level, category_id) {
    if (level < 4) {
        $("#imgClass" + level + "")[0].oninput = function () {
            console.log($("#imgClass" + level + "").val())
            if (level == 1) {
                oneCategoryId = category_id;
            } else if (level == 2) {
                twoCategoryId = category_id;
            } else if (level == 3) {
                threeCategoryId = category_id;
            }
            var user_data = {
                parentCateId: category_id,
                categoryName: $("#imgClass" + level + "").val()
            }
            var succCallBack = function (data, status, response) {
                console.log(data)
                if (data.errorCode == "0") {
                    if (data.data == null) {
                        classStr = "<li>无数据</li>";
                    } else {
                        var classStr = "";
                        for (var i = 0; i < data.data.length; i++) {
                            classStr += "<li onclick='imgNextClass(" + data.data[i].category_id + "," + data.data[i].category_level + ",\"" + data.data[i].category_name + "\")'>" + data.data[i].category_name + "</li>"
                        }
                    }
                    if (data.data[0] == undefined) {
                        classValue = category_id;
                        var nextLevel = level + 1;
                        $("#img-classUl" + nextLevel + "").html(classStr);
                        $("#img-classUl" + level + "").hide();
                    } else {
                        var nextLevel = data.data[0].category_level;
                        $("#img-classUl" + nextLevel + "").html(classStr);
                        $("#img-classUl" + level + "").show();
                    }
                }
            }
            web.doAjax(root.goodsInter.categoryByParam, 'post', user_data, succCallBack);
        }
//获取光标
        $("#imgClass" + level + "").focus(function () {
            $("#img-classUl" + level + "").show();
        });
        $(document).bind("click", function (e) {
//id为menu的是菜单，id为open的是打开菜单的按钮
            if ($(e.target).closest("#img-classUl" + level + "").length == 0 && $(e.target).closest("#imgClass" + level + "").length == 0) {
//点击id为menu之外且id不是不是open，则触发
                $("#img-classUl" + level + "").hide()
            }
        });
    }

}

function imgNextClass(category_id, level, category_name) {
    console.log(level)
    console.log(category_id)
    if (level == 1) {
        $("#imgClass2").val("");
        $("#imgClass3").val("");
    } else if (level == 2) {
        $("#imgClass3").val("");
    }
    if (level == 1) {
        oneCategoryId = category_id;
    } else if (level == 2) {
        twoCategoryId = category_id;
    } else if (level == 3) {
        threeCategoryId = category_id;
    }
    $("#imgClass" + level + "").val(category_name)
    var user_data = {
        parentCateId: category_id
    }
    var succCallBack = function (data, status, response) {
        console.log(data)
        if (data.errorCode == "0") {
            if (data.data == null) {
                classStr = "<li>无数据</li>";
            } else {
                var classStr = "";
                for (var i = 0; i < data.data.length; i++) {
                    classStr += "<li onclick='imgNextClass(" + data.data[i].category_id + "," + data.data[i].category_level + ",\"" + data.data[i].category_name + "\")'>" + data.data[i].category_name + "</li>"
                }
            }
            if (data.data[0] == undefined) {
                classValue = category_id;
                var nextLevel = level + 1;
                $("#img-classUl" + nextLevel + "").html(classStr);
                $("#img-classUl" + level + "").hide();
            } else {
                var nextLevel = data.data[0].category_level;
                $("#img-classUl" + nextLevel + "").html(classStr);
                $("#img-classUl" + nextLevel + "").show();
                $("#img-classUl" + level + "").hide()
            }
        }
    }
    imgGetNext_input(level + 1, category_id);
    web.doAjax(root.goodsInter.categoryByParam, 'post', user_data, succCallBack);
}

var totalPage = 1;

function img_list() {

    var categoryName = $("#imgClass3").val();
    var firstCategoryName = $("#imgClass1").val();
    var secondCategoryName = $("#imgClass2").val();
    if (categoryName == "" || categoryName == undefined || categoryName == null) {
        threeCategoryId = -1;
    }

    if (firstCategoryName == "" || firstCategoryName == undefined || firstCategoryName == null) {
        oneCategoryId = -1;
    }

    if (secondCategoryName == "" || secondCategoryName == undefined || secondCategoryName == null) {
        twoCategoryId = -1;
    }

    $.ajax({
        contentType: "application/json",
        type: "POST",
        url: root.goodsInter.goods_listImg,
        cache: false,  //禁用缓存
        dataType: "json",
        data: JSON.stringify({
            "pageSize": "50",
            "nowPage": 1,
            "model": {
                "spuCode": $("#spuCode").val(),
                "skuCode": $("#skuCode").val(),
                "imgName": $("#imgName").val(),
                "oneCategoryId": oneCategoryId,
                "twoCategoryId": twoCategoryId,
                "threeCategoryId": threeCategoryId
            }
        }),
        success: function (data) {
            if (data.errorCode == 0) {
                var imgHtmlStr = "";
                var objList = data.data.list;
                if(objList != null) {
                    for(var i = 0; i < objList.length; i++) {
                        if (objList[i].img_name == null || objList[i].img_name == "null") {
                            objList[i].img_name = "";
                        }
                        if (objList[i].img_url != null && objList[i].img_url != "null") {
                            if (objList[i].use_img == 0) {
                                imgHtmlStr += '<li><img onclick="picShow(\'查看图片\',\'picShow.html\',\'' + objList[i].img_url + '\',\'500\',\'300\',\'110\')" src="' + objList[i].img_url + '"><div class="num">编码： <span>' + objList[i].img_id + '</span></div><div class="name">名称： <span>' + objList[i].img_name + '</span></div><div class="sale_pic_btn"><a onclick="updataImg(' + objList[i].img_id + ')">修改</a><a onclick="deleteImg(' + objList[i].img_id + ')">删除</a></div></li>';
                            } else if (objList[i].use_img == 1) {
                                imgHtmlStr += '<li><img onclick="picShow(\'查看图片\',\'picShow.html\',\'' + objList[i].img_url + '\',\'500\',\'300\',\'110\')" src="' + objList[i].img_url + '"><div class="num">编码： <span>' + objList[i].img_id + '</span></div><div class="name">名称： <span>' + objList[i].img_name + '</span></div><div class="sale_pic_btn"></div></li>';
                            }
                        }

                    }
                    $("#imgListBox").html(imgHtmlStr);
                }
                totalPage = data.data.totalPage
                $('.M-box3').pagination({
                    pageCount: totalPage,
                    jump: true,
                    coping: true,
                    prevContent: '上页',
                    nextContent: '下页',
                    callback: function (api) {
                        console.log(api.getCurrent());
                        $.ajax({
                            contentType: "application/json",
                            type: "POST",
                            url: root.goodsInter.goods_listImg,
                            cache: false,  //禁用缓存
                            dataType: "json",
                            data: JSON.stringify({
                                "pageSize": "50",
                                "nowPage": api.getCurrent(),
                                "model": {
                                    "spuCode": $("#spuCode").val(),
                                    "skuCode": $("#skuCode").val(),
                                    "imgName": $("#imgName").val(),
                                    "oneCategoryId": oneCategoryId,
                                    "twoCategoryId": twoCategoryId,
                                    "threeCategoryId": threeCategoryId
                                }
                            }),
                            success: function (data) {
                                console.log(data);
                                if (data.errorCode == 0) {
                                    var imgHtmlStr = "";
                                    var objList = data.data.list;
                                    if(objList != null) {
                                        for(var i = 0; i < objList.length; i++) {
                                            if (objList[i].img_name == null || objList[i].img_name == "null") {
                                                objList[i].img_name = "";
                                            }
                                            if (objList[i].img_url != null && objList[i].img_url != "null") {
                                                if (objList[i].use_img == 0) {
                                                    imgHtmlStr += '<li><img onclick="picShow(\'查看图片\',\'picShow.html\',\'' + objList[i].img_url + '\',\'500\',\'300\',\'110\')" src="' + objList[i].img_url + '"><div class="num">编码： <span>' + objList[i].img_id + '</span></div><div class="name">名称： <span>' + objList[i].img_name + '</span></div><div class="sale_pic_btn"><a onclick="updataImg(' + objList[i].img_id + ')">修改</a><a onclick="deleteImg(' + objList[i].img_id + ')">删除</a></div></li>';
                                                } else if (objList[i].use_img == 1) {
                                                    imgHtmlStr += '<li><img onclick="picShow(\'查看图片\',\'picShow.html\',\'' + objList[i].img_url + '\',\'500\',\'300\',\'110\')" src="' + objList[i].img_url + '"><div class="num">编码： <span>' + objList[i].img_id + '</span></div><div class="name">名称： <span>' + objList[i].img_name + '</span></div><div class="sale_pic_btn"></div></li>';
                                                }
                                            }

                                        }
                                        $("#imgListBox").html(imgHtmlStr);
                                    }
                                    totalPage = data.data.totalPage
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                alert(XMLHttpRequest);
                            }
                        });
                    }
                });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });
}

function picShow(title, url, imgUrl) {
    window.event ? window.event.cancelBubble = true : e.stopPropagation();
    sessionStorage.setItem("imgUrl", imgUrl);
    layer_show(title, url, "800", "500");
}

function addImg() {
    layer_show("增加图片", "addPic.html", "600", "400");
}

function updataImg(imgId) {
    $.ajax({
        contentType: "application/json",
        type: "POST",
        url: root.goodsInter.goods_getImg,
        cache: false,  //禁用缓存
        dataType: "json",
        data: JSON.stringify({
            "imgId": imgId
        }),
        success: function (data) {
            console.log(data);
            if (data.errorCode == 0) {
                if (data.use_img == 0) {
                    // 未使用，随便修
                    sessionStorage.setItem("imgInfo", JSON.stringify(data.img));
                    layer_show("修改图片", "can-updataImg.html", "600", "400");
                } else if (data.use_img == 1) {
                    //已使用，只能修改名字
                    sessionStorage.setItem("imgInfo", JSON.stringify(data.img));
                    layer_show("修改图片", "cannot-updataImg.html", "600", "400");
                }
            }

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });
}

function deleteImg(imgId) {
    layer.confirm('确认要删除吗？', function (index) {
        $.ajax({
            contentType: "application/json",
            type: "POST",
            url: root.goodsInter.goods_delImg,
            cache: false,  //禁用缓存
            dataType: "json",
            data: JSON.stringify({
                "imgId": imgId
            }),
            success: function (data) {
                console.log(data);
                if (data.errorCode == 0) {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    layer.msg("删除成功", {icon: 6, time: 1000}, function () {
                        img_list();
                    });
                } else {
                    layer.msg(data.errorMessage, {icon: 5, time: 1000}, function () {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest);
            }
        });
    });
}

function checkRepeat(el, id) {
    isSubmit = 0;
    $(el).removeClass("error");
    /*if (el.value < 1 || el.value > 5) {
        isSubmit = 1;
        console.log('输入范围有误')
        $.Huimodalalert("输入范围有误", 2000);
    }*/
    var imgSorts = document.getElementsByName('imgSort');
    for (var i = 0; i < imgSorts.length; i++) {
        if (i != (id-1)) {
            var obj = imgSorts[i];
            if (Number(el.value) == Number(obj.value)) {
                isSubmit = 1;
                console.log('数字重复');
                $.Huimodalalert("数字重复", 2000);
            }
        }

    }
    // console.log(isSubmit);
}

/*校验最小起订量*/
function validateNum() {
    var miniOrder = document.getElementById('miniOrder').value;
    var miniOrderVolidate = document.getElementById('miniOrderVol').value;
    //var miniType = $("input[name='orderLimitType']:checked").val();
    /*if (Number(miniType) != 1 || Number(miniType) != 0){
        $("#orderIntegerTimes").attr("checked", "checked");
        $.Huimodalalert("限制类型为必填!", 2000);
    }*/
    if(miniOrder != '' && miniOrder != null){
        if (isNaN(miniOrder)){
            $.Huimodalalert("请输入数字!", 2000);
            document.getElementById('miniOrder').value = miniOrderVolidate;
        }
    }
}


/*校验是否为空*/
function validateNull() {
    var miniOrder = document.getElementById('miniOrder').value;

    if(miniOrder == '' || miniOrder == null || miniOrder == undefined){
        var miniOrderVolidate = document.getElementById('miniOrderVol').value;
        $.Huimodalalert("最小起订量不能为空!", 2000);
        document.getElementById('miniOrder').value = miniOrderVolidate;
    }
    var miniOrderVolidate = document.getElementById('miniOrderVol').value;
    if (Number(miniOrder) > Number(miniOrderVolidate)){
        var miniType = $("input[name='orderLimitType']:checked").val();
        if(miniType == '0'){
            if(Number(miniOrder) % Number(miniOrderVolidate) != 0){
                $.Huimodalalert("输入起订量不是正整数倍!", 2000);
                document.getElementById('miniOrder').value = miniOrderVolidate;
                sessionStorage.setItem("mini_order", miniOrderVolidate);
            } else {
                sessionStorage.setItem("mini_order", miniOrder);
            }
        } else if(miniType == '1'){
            if(Number(miniOrder) < Number(miniOrderVolidate)){
                $.Huimodalalert("输入的数量不能低于起订量的正整数!", 2000);
                document.getElementById('miniOrder').value = miniOrderVolidate;
                sessionStorage.setItem("mini_order", miniOrderVolidate);
            } else {
                sessionStorage.setItem("mini_order", miniOrder);
            }
        }
    } else {
        sessionStorage.setItem("mini_order", miniOrder);
    }
}

/*使用spu图片*/
function extendSpuImg() {
    var spuId = sessionStorage.getItem("spuId");
    $.ajax({
        type: "POST",
        url: root.goodsInter.goods_getSpuImg,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            spuId: spuId
        },
        success: function (data) {
            if (data.errorCode == 0) {
                sessionStorage.setItem("skuImgCache", JSON.stringify(data.imgList))
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            $.Huimodalalert("图片加载失败,请重新点击!", 2000);
        }
    });
    sessionStorage.removeItem("scene_lable");
    sessionStorage.removeItem("skuInfo");
    /*sessionStorage.removeItem("mini_order");
    sessionStorage.removeItem("order_limit_type");*/

    var index = layer.open({
        type: 2,
        title: "Code详情",
        content: 'sku-info.html'+'?sku_Id='+sessionStorage.getItem("spuId")
    });
    layer.full(index);
}

/*缓存label*/
function orderLimitChange() {
    var miniType = $("input[name='orderLimitType']:checked").val();
    sessionStorage.setItem("order_limit_type", miniType);
}
